package edu.hnu.commonutils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;


@Data
public class Result<T> {

  @ApiModelProperty(value = "是否成功")
  private Boolean success;

  @ApiModelProperty(value = "返回码")
  private int code;

  @ApiModelProperty(value = "返回消息")
  private String message;

  @ApiModelProperty(value = "返回数据")
  private Object data = null;

  private Result() {
  }

  public static<T> Result<T> ok() {
    Result result = new Result();
    result.setSuccess(true);
    result.setCode(ResultCode.SUCCESS);
    result.setMessage("成功");
    return result;
  }
  public static <T> Result<T> ok(T data, String message) {
    Result<T> result = new Result();
    result.setSuccess(true);
    result.setData(data);
    result.parserEnum(EnumResultCode.SUCCESS);
    //覆盖
    result.setMessage(message);
    return result;
  }

  public static<T> Result<T> error() {
    Result result = new Result();
    result.setSuccess(false);
    result.setCode(ResultCode.ERROR);
    result.setMessage("失败");
    return result;
  }
  public static <T> Result<T> error(EnumResultCode resultCode) {
    Result<T> result = new Result();
    result.setSuccess(false);
    result.parserEnum(resultCode);
    return result;
  }

  public Result data(Object obj) {
    this.setData(obj);
    return this;
  }
  public void parserEnum(EnumResultCode enumCode) {
    this.code = enumCode.getCode();// 获取枚举常量的状态码，赋值给属性
    this.message = enumCode.getMsg();// 获取枚举常量的描述信息
  }
}
